/*
 * Copyright (C) 2015 Daniel Uppenkamp
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.googlecode.maurersmarbles.desktop;

import com.googlecode.maurersmarbles.GameEngine;
import com.googlecode.maurersmarbles.GameState;
import com.googlecode.maurersmarbles.TouchEvent;
import com.googlecode.maurersmarbles.states.IntroState;
import java.awt.event.MouseEvent;

/**
 *
 * @author Daniel Uppenkamp
 */
public class MaurersMarblesDesktop extends javax.swing.JFrame {

    /**
     * The desktop screen.
     */
    protected DesktopScreen desktopScreen = new DesktopScreen();
    /**
     * The game engine.
     */
    protected GameEngine gameEngine = new GameEngine();
    /**
     * The game state.
     */
    protected GameState gameState = new GameState();

    /**
     * Creates new form MaurersMarblesDesktop
     */
    public MaurersMarblesDesktop() {
        initComponents();
        jPanel.add(desktopScreen);
    }

    @Override
    public void setVisible(boolean b) {
        super.setVisible(b);
        if (b) {
            gameEngine.setState(desktopScreen, gameState, IntroState.getInstance());
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel = new javax.swing.JPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Maurer's Marbles");
        setPreferredSize(new java.awt.Dimension(480, 800));
        getContentPane().setLayout(new javax.swing.BoxLayout(getContentPane(), javax.swing.BoxLayout.LINE_AXIS));

        jPanel.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                jPanelMousePressed(evt);
            }
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                jPanelMouseReleased(evt);
            }
        });
        jPanel.addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentResized(java.awt.event.ComponentEvent evt) {
                jPanelComponentResized(evt);
            }
        });
        jPanel.setLayout(new javax.swing.BoxLayout(jPanel, javax.swing.BoxLayout.LINE_AXIS));
        getContentPane().add(jPanel);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jPanelComponentResized(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_jPanelComponentResized

        gameEngine.handleResizeEvent(desktopScreen, gameState, getWidth(), getHeight());

    }//GEN-LAST:event_jPanelComponentResized

    private void jPanelMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jPanelMousePressed

        if (evt.getButton() == MouseEvent.BUTTON1) {
            TouchEvent touchEvent = new TouchEvent(evt.getX(), evt.getY(), TouchEvent.TYPE_PRESSED);
            gameEngine.handleInputEvent(desktopScreen, gameState, touchEvent);
        }

    }//GEN-LAST:event_jPanelMousePressed

    private void jPanelMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jPanelMouseReleased

        if (evt.getButton() == MouseEvent.BUTTON1) {
            TouchEvent touchEvent = new TouchEvent(evt.getX(), evt.getY(), TouchEvent.TYPE_RELEASED);
            gameEngine.handleInputEvent(desktopScreen, gameState, touchEvent);
        }

    }//GEN-LAST:event_jPanelMouseReleased

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new MaurersMarblesDesktop().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel jPanel;
    // End of variables declaration//GEN-END:variables
}
